package com.inputabc.ct.v1.ui.border;

import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

/**
 * 科技风格的边框
 */
public class TechBorder extends AbstractBorder {
    private static final int BORDER_THICKNESS = 2;
    // 调整圆角半径为适中大小，不过于圆滑
    private static final int CORNER_RADIUS = 30;
    private static final int CORNER_ELEMENT_SIZE = 14;
    private static final int ACCENT_BAR_WIDTH = 40;
    
    // 科技风格的颜色
    private final Color mainColor = new Color(0, 120, 255);   // 蓝色
    private final Color accentColor = new Color(80, 200, 255); // 浅蓝色
    private Color backgroundColor; // 背景色，从外部传入
    
    // 缓存的图像，避免每次重绘
    private BufferedImage cachedBorder = null;
    private int cachedWidth = 0;
    private int cachedHeight = 0;
    
    /**
     * 创建默认黑色背景的科技边框
     */
    public TechBorder() {
        this(new Color(20, 20, 20, 220)); // 默认使用半透明黑色背景
    }
    
    /**
     * 创建指定背景色的科技边框
     * @param backgroundColor 背景颜色
     */
    public TechBorder(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
        
        // 确保背景颜色有一定透明度，以保持边框效果美观
        if (backgroundColor.getAlpha() == 255) {
            // 如果是完全不透明的颜色，添加一些透明度
            this.backgroundColor = new Color(
                backgroundColor.getRed(),
                backgroundColor.getGreen(),
                backgroundColor.getBlue(),
                220); // 设置透明度为220
        }
    }
    
    /**
     * 设置背景颜色
     * @param backgroundColor 新的背景颜色
     */
    public void setBackgroundColor(Color backgroundColor) {
        // 确保背景颜色有一定透明度
        if (backgroundColor.getAlpha() == 255) {
            this.backgroundColor = new Color(
                backgroundColor.getRed(),
                backgroundColor.getGreen(),
                backgroundColor.getBlue(),
                220); // 设置透明度为220
        } else {
            this.backgroundColor = backgroundColor;
        }
        
        // 重置缓存，以便下次重绘
        cachedBorder = null;
    }
    
    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        // 如果尺寸改变或缓存为空，重新绘制边框
        if (cachedBorder == null || cachedWidth != width || cachedHeight != height) {
            cachedWidth = width;
            cachedHeight = height;
            
            // 创建一个带Alpha通道的图像，尺寸比实际大一些以避免边界问题
            cachedBorder = new BufferedImage(width + 8, height + 8, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = cachedBorder.createGraphics();
            
            // 设置渲染提示，使边缘更平滑
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            
            // 应用4像素的平移，避免边缘裁剪问题
            g2d.translate(4, 4);
            
            // 确保背景透明
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(-8, -8, width + 16, height + 16);
            g2d.setComposite(AlphaComposite.SrcOver);
            
            // 创建稍微小一些的填充区域，避免边缘露出
            int fillPadding = 1;
            g2d.setColor(backgroundColor);
            g2d.fill(new RoundRectangle2D.Float(
                    fillPadding, 
                    fillPadding, 
                    width - 2*fillPadding, 
                    height - 2*fillPadding, 
                    CORNER_RADIUS, 
                    CORNER_RADIUS));
            
            // 使用圆滑的笔触
            g2d.setStroke(new BasicStroke(BORDER_THICKNESS, 
                    BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            
            // 绘制主边框 - 使用略小的区域绘制边框，确保边框完全在填充区域内部
            drawMainBorder(g2d, fillPadding, fillPadding, width - 2*fillPadding, height - 2*fillPadding);
            
            // 绘制角落装饰元素
            drawCornerElements(g2d, fillPadding, fillPadding, width - 2*fillPadding, height - 2*fillPadding);
            
            // 绘制顶部和底部的装饰条
            drawAccentBars(g2d, fillPadding, fillPadding, width - 2*fillPadding, height - 2*fillPadding);
            
            g2d.dispose();
        }
        
        // 绘制缓存的边框图像，偏移-4像素以适应之前的平移
        g.drawImage(cachedBorder, x - 4, y - 4, null);
    }
    
    private void drawMainBorder(Graphics2D g2d, int x, int y, int width, int height) {
        // 设置边框颜色和宽度
        g2d.setColor(mainColor);
        g2d.setStroke(new BasicStroke(BORDER_THICKNESS, 
                BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        
        // 绘制超大圆角的矩形边框 - 适当收缩以避免边缘裁剪
        g2d.draw(new RoundRectangle2D.Float(
                x, y, width - 1, height - 1, 
                CORNER_RADIUS, CORNER_RADIUS
        ));
    }
    
    private void drawCornerElements(Graphics2D g2d, int x, int y, int width, int height) {
        int size = CORNER_ELEMENT_SIZE;
        
        // 设置角落元素的颜色
        g2d.setColor(accentColor);
        
        // 调整元素位置，更靠内一些，确保在圆角内部
        int offset = CORNER_RADIUS / 3;
        
        // 四个角的装饰全部使用圆形
        // 左上角装饰
        g2d.fillOval(x + offset, y + offset, size, size);
        
        // 右上角装饰
        g2d.fillOval(x + width - size - offset - 1, y + offset, size, size);
        
        // 左下角装饰
        g2d.fillOval(x + offset, y + height - size - offset - 1, size, size);
        
        // 右下角装饰
        g2d.fillOval(x + width - size - offset - 1, y + height - size - offset - 1, size, size);
    }
    
    private void drawAccentBars(Graphics2D g2d, int x, int y, int width, int height) {
        // 设置装饰条的颜色
        g2d.setColor(accentColor);
        
        // 使用更宽、更圆滑的装饰条
        int barHeight = 8;
        // 装饰条圆角半径设为高度，使其两端完全为半圆形
        int barRadius = barHeight;
        
        // 顶部中间的装饰条
        int barX = x + (width - ACCENT_BAR_WIDTH) / 2;
        g2d.fill(new RoundRectangle2D.Float(
                barX, y + 6, ACCENT_BAR_WIDTH, barHeight, 
                barRadius, barRadius
        ));
        
        // 底部中间的装饰条
        g2d.fill(new RoundRectangle2D.Float(
                barX, y + height - barHeight - 6, ACCENT_BAR_WIDTH, barHeight, 
                barRadius, barRadius
        ));
        
        // 顶部两侧小装饰
        int smallBarWidth = ACCENT_BAR_WIDTH / 3;
        int smallBarX1 = x + width / 5 - smallBarWidth / 2;
        int smallBarX2 = x + 4 * width / 5 - smallBarWidth / 2;
        
        g2d.fill(new RoundRectangle2D.Float(
                smallBarX1, y + 6, smallBarWidth, barHeight, 
                barRadius, barRadius
        ));
        
        g2d.fill(new RoundRectangle2D.Float(
                smallBarX2, y + 6, smallBarWidth, barHeight, 
                barRadius, barRadius
        ));
        
        // 底部两侧小装饰
        g2d.fill(new RoundRectangle2D.Float(
                smallBarX1, y + height - barHeight - 6, smallBarWidth, barHeight, 
                barRadius, barRadius
        ));
        
        g2d.fill(new RoundRectangle2D.Float(
                smallBarX2, y + height - barHeight - 6, smallBarWidth, barHeight, 
                barRadius, barRadius
        ));
    }
    
    @Override
    public Insets getBorderInsets(Component c) {
        // 适当增加边距以适应圆角
        return new Insets(CORNER_ELEMENT_SIZE + 12, CORNER_ELEMENT_SIZE + 12, CORNER_ELEMENT_SIZE + 12, CORNER_ELEMENT_SIZE + 12);
    }
    
    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        // 适当增加边距以适应圆角
        insets.left = insets.right = insets.top = insets.bottom = CORNER_ELEMENT_SIZE + 12;
        return insets;
    }
    
    @Override
    public boolean isBorderOpaque() {
        // 设置为不透明，确保完全覆盖
        return true;
    }
} 